﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using agent02.Areas.Admin.ViewModels;
using agent02.Model;

namespace agent02.Areas.Admin.Controllers
{
    public class WebServiceController : Controller
    {
        AgentDBEntities db = new AgentDBEntities();

        //
        // GET: /WebService/
        public ActionResult Index()
        {
            var webservice = from w in db.WEB_SERVICE
                     select w;
            return View(webservice.ToList());
        }

        //
        // GET: /WebService/Details/WS005
        public ActionResult Details(string id)
        {
            var webservice = from w in db.WEB_SERVICE
                             where w.MaWS == id
                             select w;
            return View(webservice.Single());
        }

        //
        // GET: /WebService/Create
        public ActionResult Create()
        {
            WebServiceCreateViewModel m = new WebServiceCreateViewModel();
            m.tt = new SelectList(new[] { 0, 1 });
            var provider = from p in db.NHA_CUNG_CAP
                           select p;
            m.tenNCC = new SelectList(provider, "MaNCC", "TenNCC");
            
            return View(m);
        } 

        //
        // POST: /WebService/Create
        [HttpPost]
        public ActionResult Create(WEB_SERVICE ws, FormCollection collection)
        {
            try
            {
                // auto generate MaWS
                var webservice = from w in db.WEB_SERVICE
                                 select w;
                int num = webservice.Count() + 1;
                string ma;

                if (num < 10)
                {
                    ma = "WS00";
                }
                else if (num < 100)
                {
                    ma = "WS0";
                }
                else
                {
                    ma = "WS";
                }

                ma = ma + num.ToString();

                ws.MaWS = ma;

                db.AddToWEB_SERVICE(ws);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(ws);
            }
        }
        
        //
        // GET: /WebService/Edit/WS005
        public ActionResult Edit(string id)
        {
            WebServiceCreateViewModel m = new WebServiceCreateViewModel();

            m.tt = new SelectList(new[] { 0, 1 });
            var provider = from p in db.NHA_CUNG_CAP
                           select p;
            m.tenNCC = new SelectList(provider, "MaNCC", "TenNCC");

            var webservice = from w in db.WEB_SERVICE
                             where w.MaWS == id
                             select w;
            m.ws = webservice.Single();
            return View(m);
        }

        //
        // POST: /WebService/Edit/WS005
        [HttpPost]
        public ActionResult Edit(string id, WebServiceCreateViewModel w, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var webservice = (from s in db.WEB_SERVICE
                                 where s.MaWS == id
                                 select s).Single();

                webservice.Username = w.ws.Username;
                webservice.Password = w.ws.Password;
                webservice.TrangThai = w.ws.TrangThai;
                webservice.DiaChiWS = w.ws.DiaChiWS;
                webservice.MaNCC = w.ws.MaNCC;

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                return View(w);
            }
        }

        //
        // GET: /WebService/Delete/WS005
        public ActionResult Delete(string id)
        {
            var webservice = (from s in db.WEB_SERVICE
                              where s.MaWS == id
                              select s).Single();
            return View(webservice);
        }

        //
        // POST: /WebService/Delete/WS005
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                var webservice = (from s in db.WEB_SERVICE
                                  where s.MaWS == id
                                  select s).Single();

                db.WEB_SERVICE.DeleteObject(webservice);
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
